package com.neuedu.web.common;

import com.neuedu.web.po.DeptPO;
import com.neuedu.web.po.UmsUser;
import com.neuedu.web.utils.DBUtils;
import org.junit.experimental.theories.Theories;

import java.lang.reflect.InvocationTargetException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 金山
 * 项目：jdbc-dao
 * site: https://blog.fulfill.com.cn
 * 描述
 * @data 2024/11/1115:33
 */
public class BaseDao<T > {

    protected ThreadLocal<Long> threadLocal = new ThreadLocal<>();

    public int update(String  sql ,Object... params){

        //变成小写sql
        sql = sql.toLowerCase();

        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        int count = 0;
        try {
            conn = DBUtils.getConnection();
            if(sql.indexOf("insert") != -1){ //插入数据需要返回  自增主键
                stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            }else{
                stmt = conn.prepareStatement(sql);
            }

            //绑定变量
            DBUtils.bingStmtVar(stmt,params);
            //执行查询
            count = stmt.executeUpdate();


            //获取新增的主键
            if(sql.indexOf("insert") != -1){
                ResultSet generatedKeys = stmt.getGeneratedKeys();
                if(generatedKeys.next()){
                    long id = generatedKeys.getLong(1);
                    threadLocal.set(id);
                }
            }


        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return  count;
    }

    /**
     * 查询集合
     * @param clazz
     * @param sql
     * @param params
     * @return
     * @throws NoSuchFieldException
     * @throws InvocationTargetException
     * @throws NoSuchMethodException
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
    public List<T> selectAll(Class<T> clazz ,String sql, Object... params) throws NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {

        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        List list = new ArrayList();
        try {
            conn = DBUtils.getConnection();
            stmt = conn.prepareStatement(sql);


            // 绑定变量
            DBUtils.bingStmtVar(stmt, params);

            // 执行查询
            rs = stmt.executeQuery();
            while (rs.next()) {

                T rowPo = DBUtils.analysisResultSet2PO(rs, clazz);
                list.add(rowPo);

            }


        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }


    public  T selectOne(Class<T> clazz ,String sql, Object... params) throws NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {

        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;


        T target = null;


        try {
            conn = DBUtils.getConnection();
            stmt = conn.prepareStatement(sql);


            // 绑定变量
            DBUtils.bingStmtVar(stmt, params);

            // 执行查询
            rs = stmt.executeQuery();
            if (rs.next()) {
                target = DBUtils.analysisResultSet2PO(rs, clazz);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return target;
    }



    public int total(String sql, Object... params) {

        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        int count = 0;
        try {
            conn = DBUtils.getConnection();
            stmt = conn.prepareStatement("select count(1) c from (" + sql + ") tab  ");

            // 绑定变量
            DBUtils.bingStmtVar(stmt, params);
            // 执行查询
            rs = stmt.executeQuery();

            // 定位到数据行
            if (rs.next()) {
                count = rs.getInt("c");
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return count;
    }


    /**
     * 分页查询
     * @param clazz
     * @param current
     * @param size
     * @param sql
     * @param params
     * @return
     * @throws NoSuchFieldException
     * @throws InvocationTargetException
     * @throws NoSuchMethodException
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
    public List<T> selectPage(Class<T> clazz , int current, int size, String sql, Object... params) throws NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {

        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        List list = new ArrayList();
        try {
            conn = DBUtils.getConnection();

            int start = (current - 1) * size;
            int len = size;
            String limitSql = "select *  from ("+sql+") tab limit  "+start+"  , "+len;
            System.out.println("分页sql: "+limitSql);
            stmt = conn.prepareStatement(limitSql);


            // 绑定变量
            DBUtils.bingStmtVar(stmt, params);

            // 执行查询
            rs = stmt.executeQuery();
            while (rs.next()) {

                T row = DBUtils.analysisResultSet2PO(rs,clazz);

                list.add(row);

            }


        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return list;
    }




}
